package cn.seecoder.courselearning.serviceimpl.test;

import cn.seecoder.courselearning.mapperservice.test.SubmitMapper;
import cn.seecoder.courselearning.po.test.Submit;
import cn.seecoder.courselearning.service.test.SubmitService;
import cn.seecoder.courselearning.util.Constant;
import cn.seecoder.courselearning.vo.ResultVO;
import cn.seecoder.courselearning.vo.test.SubmitVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class SubmitServiceImpl implements SubmitService {

    @Resource
    private SubmitMapper submitMapper;

    @Override
    public ResultVO<SubmitVO> createSubmit(SubmitVO submitVO) {
        Submit submit = new Submit(submitVO);

        Integer uid = submit.getUid();
        Integer tid = submit.getTid();
        Integer qid = submit.getQid();

        //重复提交：若已提交过，需删除之前的提交记录
        if(submitMapper.selectByPrimaryKey(uid, tid, qid) != null){
            submitMapper.deleteByPrimaryKey(uid, tid, qid);
        }

        if(submitMapper.insert(submit) > 0){
            return new ResultVO<>(Constant.REQUEST_SUCCESS, "提交成功", submitVO);
        }
        else{
            return new ResultVO<>(Constant.REQUEST_FAIL, "提交失败");
        }
    }

    /**
     * 通过用户id、测试id、题目id得到测试记录
     * @param uid 用户id
     * @param tid 测试id
     * @param qid 题目id
     * @return
     */
    public SubmitVO getSubmitByUTQId(int uid, int tid, int qid){
        Submit submit = submitMapper.selectByPrimaryKey(uid, tid, qid);
        return new SubmitVO(submit);
    }
}
